package com.offcn.web;

import com.offcn.entity.Course;
import com.offcn.service.CourseService;
import com.offcn.service.CourseServiceImpl;
import com.offcn.utils.*;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@WebServlet("/course")
@MultipartConfig    //处理文件上传数据
public class CourseServlet extends BaseServlet {

    private ResultVO resultVO;
    private CourseService courseService = new CourseServiceImpl();

    /**
     * 文件上传注意：
     * 1，表单必须支持二进制的文件数据entype="mi"
     * 2,表单的提交方式必须是post
     */

    public void uploadFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //要拿的是文件 不是字符串  拿二进制的数据不要用getParameter方法
        Part myFile = req.getPart("myFile");

        try {
            String fileName = FileUploadUtils.upload(req);
            if (fileName.contains(".jpg")||fileName.contains(".png")||fileName.contains(".jpeg")){
                resultVO = new ResultVO(201,"上传数据成功",fileName);
            }else {
                resultVO = new ResultVO(202,"上传视频成功",fileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO(500,"上传失败",null);
        }finally {
            JsonUtils.toPrint(resultVO,resp);
        }
        //上传文件  1.文件名不能重名  2.指定文件保存位置  3.写入到硬盘中
        //获取文件名
        //String fileName = myFile.getSubmittedFileName();    //xxx.jpg
        //生成不会重复的文件名
        //fileName=System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
        //指定保存的位置
        //String path ="C:\\Users\\Administrator\\Desktop\\upload\\";
        //写入到硬盘
        //myFile.write(path+fileName);
        //把文件名返回去 响应文件名给axios,实现上传资源回显
        //JsonUtils.toPrint(new ResultVO(200,"上传成功",fileName),resp);

    }


    public void removeFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("removeFile方法");
        String fileName = req.getParameter("fileName");
        //使用File找到文件 执行delete方法
        //File file = new File("C:\\Users\\Administrator\\Desktop\\upload",fileName);
        //从linux里面移除
        File file = new File("/upload",fileName);
        boolean b = file.delete();
        if (b){
            resultVO=new ResultVO(200,"删除成功",null);
        }else {
            resultVO=new ResultVO(500,"删除失败",null);
        }
        JsonUtils.toPrint(resultVO,resp);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Course course = new Course();
        BeanUtils.populate(course,req.getParameterMap());
        Boolean b= courseService.add(course);
        if (b){
            resultVO=new ResultVO(200,"添加成功",null);
        }else {
            resultVO=new ResultVO(500,"添加失败",null);
        }
        JsonUtils.toPrint(resultVO,resp);
    }

    public void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        String search = req.getParameter("search");

        PageUtils<Course> pageUtils = courseService.findByPage(currentPage,pageSize,search);

        resultVO=new ResultVO(200,"查询成功",pageUtils);

        JsonUtils.toPrint(resultVO,resp);

    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Course course = new Course();
        BeanUtils.populate(course,req.getParameterMap());

        Boolean b= courseService.update(course);
        if (b){
            resultVO=new ResultVO(200,"修改成功",null);
        }else {
            resultVO=new ResultVO(500,"修改失败",null);
        }
        JsonUtils.toPrint(resultVO,resp);
    }

    public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        Boolean b=courseService.del(cid);
        if (b){
            resultVO=new ResultVO(200,"删除成功",null);
        }else {
            resultVO=new ResultVO(500,"删除失败",null);
        }

        JsonUtils.toPrint(resultVO,resp);
    }

    //查询所有课程
    public void findAllCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Course> courseList =courseService.findAllCourse();
        if (courseList.size()>0){
            resultVO=new ResultVO(200,"查询成功",courseList);
        }else {
            resultVO=new ResultVO(500,"删除失败",courseList);
        }

        JsonUtils.toPrint(resultVO,resp);
    }
}
